class ManagerBooksController < AdminController
  def indexbooks
    @books = Book.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @books }
    end
  end

  def showbooks
    @categories = Category.all
    @book = Book.find(params[:id])
    @category = Category.find(@book.category_id)

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @book }
    end
  end

  def newbooks
    @book = Book.new
    @categories = Category.all

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @book }
    end
  end

  def editbooks
    @book = Book.find(params[:id])
    @categories = Category.all
  end

  def createbooks
    @book = Book.new(params[:book])
    @categories = Category.all

    if(params[:image] != nil)
      @book.image_url = params[:image].original_filename
    end
    @book.numviewed = 0
    @book.numborrowed = 0
    @book.current_quantity = @book.quantity

    respond_to do |format|
      if @book.save

        if(params[:image] != nil)
          uploaded_io = params[:image]
          File.open(Rails.root.join('app/assets/images', uploaded_io.original_filename), "wb") do |file|
            file.write(uploaded_io.read)
          end
        end

        format.html { redirect_to showbooks_manager_books_url(@book), notice: 'Book was successfully created.' }
        format.json { render json: @book, status: :created, location: @book }
      else
        format.html { render action: "newbooks" }
        format.json { render json: @book.errors, status: :unprocessable_entity }
      end
    end
  end

  def updatebooks
    @book = Book.find(params[:id])
    @categories = Category.all

    if(params[:image] != nil)
      @book.image_url = params[:image].original_filename
    end
    old_quantity = @book.quantity
    new_quantity = params[:book][:quantity]
    delta = new_quantity.to_i - old_quantity
    respond_to do |format|
      if @book.update_attributes(params[:book])

        if(params[:image] != nil)
          uploaded_io = params[:image]
          File.open(Rails.root.join('app/assets/images', uploaded_io.original_filename), "wb") do |file|
            file.write(uploaded_io.read)
          end
        end

        @book.current_quantity = @book.current_quantity.to_i + delta
        @book.save
        format.html { redirect_to showbooks_manager_books_url(@book), notice: 'Book was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "editbooks" }
        format.json { render json: @book.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroybooks
    @book = Book.find(params[:id])
    @book.destroy

    respond_to do |format|
      format.html { redirect_to indexbooks_manager_books_url }
      format.json { head :no_content }
    end
  end

  def delete_multiple_books
    if(params[:book_ids] != nil)
      @books = Book.find(params[:book_ids])
      @books.each do |book|
        book.destroy
      end
    end

    respond_to do |format|
      format.html { redirect_to indexbooks_manager_books_url }
      format.json { head :no_content }
    end
  end

  def searchbooks
    @books = Book.find(:all, :conditions => ["code LIKE ? AND title LIKE ? AND author LIKE ?", "%" + params[:search_idbook] + "%", "%" + params[:search_title] + "%", "%" + params[:search_author] + "%"])

    respond_to do |format|
      format.html
      format.json { head :no_content }
    end
  end
end
